package se.lnu.axez;

import java.util.ArrayList;
import java.util.List;

import se.lnu.axez.shaders.ShaderManager;
import se.lnu.axez.shaders.ShaderManager.ShaderFactory;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
import android.widget.Toast;

public final class AxezView extends GLSurfaceView {
	/**
	 * Default camera which looks at the scene center from point {0, 0, -20}.
	 * Positive Y axis is top.
	 */
	private static final Camera DEFAULT_CAMERA;
	
	static {
		DEFAULT_CAMERA = Camera.createNew(60f, 1f, 200f);
		DEFAULT_CAMERA.lookAt(0, 0, 20, 0, 0, 0, 0, 1, 0);
	}
	
	private final List<IControl> controls = new ArrayList<IControl>();
	private final AxezRenderer mRenderer;
	
	public AxezView(Context context) {
		super(context);
		
		this.mRenderer = new AxezRenderer(this.controls, context, DEFAULT_CAMERA);

		super.setEGLConfigChooser(true);
        super.setEGLContextClientVersion(2);
        super.setRenderer(this.mRenderer);
	}
	
	public void setCamera(Camera camera) {
		mRenderer.setActiveCamera(camera);
	}
	
	public void addControl(IControl control) {
		if (control == null) {
			throw new IllegalArgumentException();
		}
		
		this.controls.add(control);
	}
	
	public void setActiveCamera(Camera camera) {
		if (camera == null) {
			return;
		}
		
		mRenderer.setActiveCamera(camera);
	}
	
	public void setOnPickListener(IObjectPickListener listener) {
		this.mRenderer.setOnPickListener(listener);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mRenderer.onTouchEvent(event);
    	return true;
	}
}
